﻿#include "ChineseHistoryTimeLine.h"
#include "ui_ChineseHistoryTimeLine.h"
#include "ChineseHistoryTools.h"
#include <QFile>

ChineseHistoryTimeLine::ChineseHistoryTimeLine(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChineseHistoryTimeLine)
{
    ui->setupUi(this);
    ui->stackedWidget->setCurrentIndex(0);
    initComboBoxContent();

    connect(ui->pushButton_search, SIGNAL(clicked()), this, SLOT(slotSetHistoryDataChart()));
    connect(ui->pushButton_return, SIGNAL(clicked()), this, SLOT(slotReturnHomePage()));
    connect(ui->pushButton_change, SIGNAL(clicked()), this, SLOT(slotChangeSearchStyle()));
    connect(ui->comboBox_country, SIGNAL(currentIndexChanged(QString)), this, SLOT(slotRefreshEmperorComboList(QString)));

    // 网页加载完成后刷新图表
    connect(ui->webEngineView, SIGNAL(loadFinished(bool)), this, SLOT(slotWebLoadFinish(bool)));
    m_webFlag = 0;
    m_data = "";

}

ChineseHistoryTimeLine::~ChineseHistoryTimeLine()
{
    delete ui;
}

// 根据查询情况对应选择不同图表
void ChineseHistoryTimeLine::slotSetHistoryDataChart()
{
    if(ui->stackedWidget->currentIndex() == 0)  // 按照起止时间查询
    {
        int begin = ui->lineEdit_startTime->text().toInt();
        int end = ui->lineEdit_endTime->text().toInt();
        showChartByTime(begin, end);
    }
    else if(ui->stackedWidget->currentIndex() == 1)  // 按照朝代或者皇帝查询
    {
        QString strEmperor = ui->comboBox_emperor->currentText();
        QString strCountry = ui->comboBox_country->currentText();
        if(strEmperor != "----")  // 根据朝代和皇帝刷新图表
        {
            showChartByCountryAndEmperor(strCountry, strEmperor);
        }
        else if(strCountry != "----")  // 根据朝代刷新图表
        {
            showChartByCountry(strCountry);
        }
    }



}

// 返回主页
void ChineseHistoryTimeLine::slotReturnHomePage()
{
    this->hide();
}

// 切换查询方式
void ChineseHistoryTimeLine::slotChangeSearchStyle()
{
    if(ui->stackedWidget->currentIndex() == 0)
        ui->stackedWidget->setCurrentIndex(1);
    else if(ui->stackedWidget->currentIndex() == 1)
        ui->stackedWidget->setCurrentIndex(0);
}

// 根据选择的朝代刷新皇帝下拉列表
void ChineseHistoryTimeLine::slotRefreshEmperorComboList(QString)
{
    ui->comboBox_emperor->clear();
    ui->comboBox_emperor->addItem("----");
    QString country = ui->comboBox_country->currentText();
    if(country == "----")
        ui->comboBox_emperor->setCurrentIndex(0);
    else
    {
        QList<EmperorReginTime> lst = ChineseHistoryTools::GetInstance()->getEmperorReginTime(country);
        for(int i = 0; i < lst.size(); ++i)
            ui->comboBox_emperor->addItem(lst.at(i).name);
    }
}

// 初始化下拉框内容
void ChineseHistoryTimeLine::initComboBoxContent()
{
    ui->comboBox_emperor->addItem("----");
    ui->comboBox_country->addItem("----");
    QStringList lst = ChineseHistoryTools::GetInstance()->getCountryList();
    for(int i = 0; i < lst.size(); ++i)
        ui->comboBox_country->addItem(lst.at(i));
}

// 根据起止时间刷新图表 --- todo
void ChineseHistoryTimeLine::showChartByTime(int begin, int end)
{
    m_webFlag = 0;

    QString htmlPath = "./Resourse/ChineseHistoryCharts/ChineseHistoryChart.html";        //从此html文件中读取内容后写入webview
    QUrl baseUrl = "file:///./Resourse/ChineseHistoryCharts/ChineseHistoryChart.html";     //外部对象，包括以下CSS和js文件
    QFile file(htmlPath);
    if (!file.open(QIODevice::ReadOnly))
    {
        return;
    }
    QString htmlData = file.readAll().constData();
    file.close();
    ui->webEngineView->setHtml(htmlData,baseUrl);
}

// 根据朝代刷新图表 --- todo 切换甘特图
void ChineseHistoryTimeLine::showChartByCountry(QString country)
{
    m_webFlag = 1;

    QList<EmperorReginTime> lst = ChineseHistoryTools::GetInstance()->getEmperorReginTime(country);
    QString pieData;  // 饼状图数据源  格式为：GGGG:20@SSSS:30@TTTT:10@VVVV:60
    for(int i = 0; i < lst.size(); ++i)
    {
        pieData += lst.at(i).name + ":" + QString("%1").arg(lst.at(i).time);
        if(i == lst.size() - 1)
            break;
        pieData += "@";
    }
    qDebug() << "pieData" << pieData;
    m_data = pieData;


    QString htmlPath = "./Resourse/ChineseHistoryCharts/EmperorReignTime.html";        //从此html文件中读取内容后写入webview
    QUrl baseUrl = "file:///./Resourse/ChineseHistoryCharts/EmperorReignTime.html";     //外部对象，包括以下CSS和js文件
    QFile file(htmlPath);
    if (!file.open(QIODevice::ReadOnly))
    {
        return;
    }
    QString htmlData = file.readAll().constData();
    file.close();    

    ui->webEngineView->setHtml(htmlData,baseUrl);

}

// 根据朝代和皇帝刷新图表 --- todo 将数据传到html
void ChineseHistoryTimeLine::showChartByCountryAndEmperor(QString country, QString emperor)
{
    m_webFlag = 2;

    QList<CountryInfo> lst = ChineseHistoryTools::GetInstance()->getSimpleCountryInfo(country, emperor);
    QString barData;  // 柱状图数据源  格式为：GGGG:20:30@SSSS:30:30@TTTT:10@VVVV:60:30  国家:起始时间:结束时间
    for(int i = 0; i < lst.size(); ++i)
    {
        barData += lst.at(i).name + ":" + QString("%1").arg(lst.at(i).start_time) + ":" + QString("%1").arg(lst.at(i).end_time);
        if(i == lst.size() - 1)
            break;
        barData += "@";
    }
    qDebug() << "barData = " << barData;
    m_data = barData;


    QString htmlPath = "./Resourse/ChineseHistoryCharts/EmperorTimeCurrentCountry.html";        //从此html文件中读取内容后写入webview
    QUrl baseUrl = "file:///./Resourse/ChineseHistoryCharts/EmperorTimeCurrentCountry.html";     //外部对象，包括以下CSS和js文件
    QFile file(htmlPath);
    if (!file.open(QIODevice::ReadOnly))
    {
        return;
    }
    QString htmlData = file.readAll().constData();
    file.close();
    ui->webEngineView->setHtml(htmlData,baseUrl);
}


// 网页加载数据
void ChineseHistoryTimeLine::slotWebLoadFinish(bool)
{
    if(m_webFlag == 0)  // 根据起止时间刷新图表
    {

    }
    else if(m_webFlag == 1)  // 根据朝代刷新图表
    {
        QString str = QString("setPieData(\"%1\")").arg(m_data);
        qDebug() << "str" << str;
        ui->webEngineView->page()->runJavaScript(str);
    }
    else if(m_webFlag == 2)  // 根据朝代和皇帝刷新图表
    {
        QString str = QString("setBarData(\"%1\",\"%2\",\"%3\")").arg(m_data).arg(ui->comboBox_emperor->currentText()).arg(ui->comboBox_country->currentText());
        qDebug() << "str" << str;
        ui->webEngineView->page()->runJavaScript(str);
    }
}
